﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen

Public Class DoSinhHieu

    Private Sub DoSinhHieu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TiepNhanBenhNhan)
        Me.DMBenhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenh)
        Me.DMTinhTrangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMTinhTrang)
        Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
        Me.DoSinhHieuTienSuBenhTableAdapter.Fill(Me.PCV_CLINICDataSet.DoSinhHieuTienSuBenh)
        Me.DoSinhHieuTableAdapter.Fill(Me.PCV_CLINICDataSet.DoSinhHieu)
        Me.IGridControlDs01.ForceInitialize()
        Me.IGridViewDs01.MoveLast()
        Me.setHeaderFooter("Công Ty CP Tư Vấn Giải Pháp ERP Phong Cách Việt", "Lâm Văn Bền, P.Tân Kiểng, Q.7, TP.HCM")
        SplashScreenManager.CloseForm()
    End Sub

    Public Overrides Sub setSubSau_TaoMoi()
        Me.NgaydoIDateEdit.EditValue = Today
        Me.setMacDinhCotAn("ma", "DSH" & System.Guid.NewGuid().ToString)
    End Sub
    Public Sub New()
        InitializeComponent()
        Me.setSoTabThamChieu(1)
        Me.setSoTab(1)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.DoSinhHieu, Me.DoSinhHieuBindingSource, Me.TableAdapterManager, Me.DoSinhHieuTableAdapter, Me.MasophieuITextEdit, Me.TinhtrangISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.DoSinhHieuTienSuBenh, Me.FKDoSinhHieuTienSuBenhDoSinhHieuBindingSource, Me.DoSinhHieuTienSuBenhTableAdapter)
        Me.IGridViewChiTiet01.setBatLoi("ma", "not null")
    End Sub

    Private Sub MabenhnhanISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, DevExpress.XtraEditors.SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MabenhnhanISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.TuoiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tuoi")
        Me.NgaysinhIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaythangnamsinh")
        Me.GioitinhITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "gioitinh")
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
        Me.SobhytITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "sobhyt")
        Me.HanbhytIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngayhethan")
    End Sub

    Private Sub IGridViewChiTiet01_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("masophieu", Me.MasophieuITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit1_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit1.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mabenh", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tenbenh", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
    End Sub
    'Nhảy xuống NgaydoIDateEdit
    Private Sub MabenhnhanISearchLookupEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MabenhnhanISearchLookupEdit.KeyDown
        If Me.getState = 1 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.HanbhytIDateEdit.Focus()
        End If
    End Sub

    Private Shadows Sub BarButtonItemBaoCao_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Dim report As XtraReport_DoSinhHieu = New XtraReport_DoSinhHieu
        report.DoSinhHieuTableAdapter.FillMa_DoSinhHieu(report.PcV_CLINICDataSet1.DoSinhHieu, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        'report.NgayThangNam.Value = "Ngày " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(0, 2) & " tháng " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(3, 2) & " năm " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(6, 4)
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaydoIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaydoIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaydoIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview() 'hiển thị report theo kiểu ribbon

    End Sub
    ' Tính BMI khi chiều cao thay đổi
    Private Sub ChieucaoITextEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles ChieucaoITextEdit.EditValueChanged
        If Me.getState <> 0 AndAlso Not IsDBNull(Me.ChieucaoITextEdit.EditValue) AndAlso Not IsDBNull(Me.CannangITextEdit.EditValue) AndAlso Not IsNothing(Me.ChieucaoITextEdit.EditValue) AndAlso Not IsNothing(Me.CannangITextEdit.EditValue) AndAlso Me.ChieucaoITextEdit.IsNeedFocus AndAlso CInt(ChieucaoITextEdit.EditValue) > 0 Then
            Dim cannang As Decimal = CDec(Me.CannangITextEdit.EditValue)
            Dim chieucao As Decimal = CDec(Me.ChieucaoITextEdit.EditValue) / 100
            Dim bmi As Decimal = CDec(cannang / (chieucao * chieucao))
            Me.BmiITextEdit.EditValue = bmi
        End If
    End Sub
    ' Tính BMI khi cân nặng thay đổi
    Private Sub CannangITextEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles CannangITextEdit.EditValueChanged
        If Me.getState <> 0 AndAlso Not IsDBNull(Me.ChieucaoITextEdit.EditValue) AndAlso Not IsDBNull(Me.CannangITextEdit.EditValue) AndAlso Not IsNothing(Me.ChieucaoITextEdit.EditValue) AndAlso Not IsNothing(Me.CannangITextEdit.EditValue) AndAlso Me.CannangITextEdit.IsNeedFocus AndAlso CInt(ChieucaoITextEdit.EditValue) > 0 Then
            Dim cannang As Decimal = CDec(Me.CannangITextEdit.EditValue)
            Dim chieucao As Decimal = CDec(Me.ChieucaoITextEdit.EditValue) / 100
            Dim bmi As Decimal = CDec(cannang / (chieucao * chieucao))
            Me.BmiITextEdit.EditValue = bmi
        End If
    End Sub
    'Lấy từ danh sách tiepnhanbenhnhan tham chiếu qua phần thông tin
    Private Sub IGridViewDsThamChieu1_DoubleClick(sender As System.Object, e As System.EventArgs) Handles IGridViewDsThamChieu1.DoubleClick
        If getState() <> 0 Then
            Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mabenhnhan")
            Me.TenITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ten")
            Me.TuoiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("tuoi")
            Me.GioitinhITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("gioitinh")
            Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("diachi")
            Me.MachITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mach")
            Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("sobhyt")
            Me.HanbhytIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ngayhethan")
        End If
    End Sub

    Private Sub ChọnBệnhNhânToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ChọnBệnhNhânToolStripMenuItem.Click
        If getState() = 1 Then
            Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mabenhnhan")
            Me.TenITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ten")
            Me.TuoiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("tuoi")
            Me.GioitinhITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("gioitinh")
            Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("diachi")
            Me.MachITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("mach")
            Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("sobhyt")
            Me.HanbhytIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ngayhethan")
        End If
    End Sub
End Class
